<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 作物基本知识
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-select v-model="queryParams.cropType" placeholder="作物类型" class="handle-select mr10">
                    <el-option key="1" label="植物" value="0"></el-option>
                    <el-option key="2" label="动物" value="1"></el-option>
                </el-select>
                <el-input v-model="queryParams.cropName" placeholder="作物名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <div>
                <el-table
                        ref="multipleTableRef"
                        :data="knowList"
                        style="width: 100%"
                        @selection-change="handleSelectionChange"
                >
                    <el-table-column type="selection" width="55"/>
                    <el-table-column prop="cropName" label="作物名称" align="center" width="180" >
                    </el-table-column>
                    <el-table-column prop="cropType" label="作物类型" align="center" width="180">
                        <template v-slot="scope">
                            <span>{{ plantationType(scope.row.cropType) }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="growth" label="最适生长期" align="center" width="180">
                    <template v-slot="scope">
                        <span>{{ scope.row.growth+'天' }}</span>
                    </template>
                    </el-table-column>
                    <el-table-column prop="temp" label="最适温度" align="center" width="180">
                        <template v-slot="scope">
                            <span>{{ scope.row.temp+'℃' }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="humidity" label="最适湿度" align="center" width="180">
                        <template v-slot="scope">
                            <span>{{ scope.row.humidity+'RH%' }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="n" label="最适含氮量" align="center" width="180">
                        <template v-slot="scope">
                            <span>{{ scope.row.n+'mg/kg' }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="k" label="最适含钾量" align="center" width="180">
                        <template v-slot="scope">
                            <span>{{ scope.row.k+'mg/kg' }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="p" label="最适含磷量" align="center" width="180">
                        <template v-slot="scope">
                            <span>{{ scope.row.p+'mg/kg' }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="light" label="最适光照度" align="center" width="180">
                        <template v-slot="scope">
                            <span>{{ scope.row.light+'%' }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="ph" label="最适ph值" align="center" width="180">
                        <template v-slot="scope">
                            <span>{{ scope.row.ph+'ph' }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="wind" label="最适风速" align="center" width="180">
                        <template v-slot="scope">
                            <span>{{ scope.row.wind+'km/h' }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="cropIntroduce" label="基本介绍" align="center" />
                </el-table>

            </div>
            <div class="pagination">
                <el-pagination background layout="total, prev, pager, next" :current-page="queryParams.pageIndex"
                               :page-size="queryParams.pageSize" :total="pageTotal"
                               @current-change="handlePageChange"></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" v-model="editVisible" width="30%">
            <el-form label-width="70px">
                <el-form-item label="用户名">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="地址">
                    <el-input v-model="form.address"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="editVisible = false">取 消</el-button>
                    <el-button type="primary" @click="saveEdit">确 定</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script>
    import {ref, reactive} from "vue";
    import {ElMessage, ElMessageBox} from "element-plus";
    import {getKnowList} from "../api/index.js";
    import { useRouter } from "vue-router";

    export default {
        name: "know",
        components: {},
        data() {
            return {
                pageTotal:'',
                knowList: [],
                queryParams:{
                    cropName:'',
                    cropType:'',
                    pageIndex: 1,
                    pageSize: 10,
                },
            }
        },
        created() {
            // this.name = localStorage.getItem("ms_username");
            let userId = localStorage.getItem("user_id");
            console.log(userId);
            this.getList();
        },
        methods: {
            getList(){
                // 通过id获取用户所有信息
                getKnowList(this.queryParams).then(res => {
                    this.knowList = res.rows;
                    this.pageTotal = res.total;
                    console.log(this.knowList);
                });
            },
            // 种植园类型转换
            plantationType(status) {
                if (status == 0) {
                    return "植物"
                } else if (status == 1) {
                    return "动物"
                }
            },
            handleSelectionChange() {
            },
            handleEdit() {
            },
            handleDelete() {
            },
            // 跳转至当前园区的经济作物列表
            // forward(plantationId){
            //     console.log("????");
            //     router.push({
            //         path: '/crop',
            //         query: {
            //             plantationId:plantationId
            //         }
            //     })
            // }
            handlePageChange(val){
                this.queryParams.pageIndex = val;
                this.getData();
            },
        },
        setup() {
            // const query = reactive({
            //     address: "",
            //     name: "",
            //     pageIndex: 1,
            //     pageSize: 10,
            // });
            const tableData = ref([]);
            const pageTotal = ref(0);
            // 获取表格数据
            const getData = () => {
                // fetchData(query).then((res) => {
                //     tableData.value = res.list;
                //     pageTotal.value = res.pageTotal || 50;
                // });
            };
            getData();

            // 查询操作
            const handleSearch = () => {
                query.pageIndex = 1;
                getData();
            };
            // // 分页导航
            // const handlePageChange = (val) => {
            //     query.pageIndex = val;
            //     getData();
            // };

            // 删除操作
            const handleDelete = (index) => {
                // 二次确认删除
                ElMessageBox.confirm("确定要删除吗？", "提示", {
                    type: "warning",
                })
                    .then(() => {
                        ElMessage.success("删除成功");
                        tableData.value.splice(index, 1);
                    })
                    .catch(() => {
                    });
            };

            // 表格编辑时弹窗和保存
            const editVisible = ref(false);
            let form = reactive({
                name: "",
                address: "",
            });
            let idx = -1;
            const handleEdit = (index, row) => {
                idx = index;
                Object.keys(form).forEach((item) => {
                    form[item] = row[item];
                });
                editVisible.value = true;
            };
            const saveEdit = () => {
                editVisible.value = false;
                ElMessage.success(`修改第 ${idx + 1} 行成功`);
                Object.keys(form).forEach((item) => {
                    tableData.value[idx][item] = form[item];
                });
            };
            const router = useRouter();
            const forward = (plantationId) => {
                router.push({
                    path: '/crop',
                    query: {
                        plantationId:plantationId
                    }
                })
            }
            return {
                // query,
                tableData,
                pageTotal,
                editVisible,
                form,
                handleSearch,
                // handlePageChange,
                handleDelete,
                handleEdit,
                forward,
                saveEdit,
            };
        },
    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }

    .table {
        width: 100%;
        font-size: 14px;
    }

    .red {
        color: #ff0000;
    }

    .mr10 {
        margin-right: 10px;
    }

    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }
</style>
